package com.hrms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hrms.dao.*;
import com.hrms.pojo.BasePage;
import com.hrms.pojo.EmpInformation;
import com.hrms.service.EmpInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * 员工基本信息 服务实现类
 *
 * @author LK
 * @since 2021-10-19
 */
@Service
public class EmpInformationServiceImpl  extends BasePage implements EmpInformationService {
//注入mapper
    @Autowired(required = false)
    EmpInformationMapper empInformationMapper;
    @Autowired(required = false)
    FileManageMapper fileManageMapper;
    @Autowired(required = false)
    ContractManageMapper contractManageMapper;
    @Autowired(required = false)
    PersonnelTransferMapper personnelTransferMapper;
    @Autowired(required = false)
    AttenManageMapper attenManageMapper;
    @Autowired(required = false)
    SalaryInfoMapper salaryInfoMapper;
    /*==============增加=================*/
    @Override
    public HashMap<String, Object> add(EmpInformation emp) {
       HashMap<String,Object> map=new HashMap<>();
       //新增
        int num=empInformationMapper.insert(emp);
        if (num>0){
            map.put("info","新增成功");
        }else {
            map.put("info","新增失败");
        }
        return map;
    }
    /*================查找=================*/
    @Override
    public HashMap<String, Object> select(EmpInformation emp) {
        HashMap<String,Object> map=new HashMap<>();
        //定义分页对象
        Page<EmpInformation> page=new Page<>(emp.getPage(),emp.getRow());
        //条件构造器
        QueryWrapper<EmpInformation> queryWrapper=new QueryWrapper<>();

        queryWrapper.eq(emp.getStaffId()!=null,"staff_id",emp.getStaffId());
        queryWrapper.like(emp.getStaffName()!=null&&!emp.getStaffName().equals(""),"staff_name",emp.getStaffName());

        //查询数据库
        Page<EmpInformation> aList= empInformationMapper.selectPage(page,queryWrapper);
        //当前页集合
        map.put("list",aList.getRecords());
        //总页数
        map.put("totalPage",aList.getPages());
        //总条数
        map.put("total",aList.getTotal());
        //当前页
        map.put("curPage",emp.getPage());
        if(emp.getPage()==1){
            //上一页
            map.put("prePage",1);

        }else{
            map.put("prePage",emp.getPage()-1);

        }
        if(emp.getPage()==aList.getPages()){
            //下一页
            map.put("nextPage",aList.getPages());

        }else{
            map.put("nextPage",emp.getPage()+1);

        }
        //每页显示条数
        map.put("row",emp.getRow());

        return map;
    }
    /*================修改=================*/
    @Override
    public HashMap<String, Object> update(EmpInformation emp) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //修改
        int num = empInformationMapper.updateById(emp);
        if(num>0){
            map.put("info","修改成功");
        }else{
            map.put("info","修改失败");
        }
        return map;
    }
    /*================删除=================*/
    @Override
    public HashMap<String, Object> del(Integer a) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num1 = fileManageMapper.deleteByStaffId(a);
        int num2 = contractManageMapper.deleteByStaffId(a);
        int num3 = personnelTransferMapper.deleteByStaffId(a);
        int num4 = empInformationMapper.deleteByStaffId(a);
        int num5 = attenManageMapper.deleteByStaffId(a);
        int num6 = salaryInfoMapper.deleteByStaffId(a);
        if((num1>0)||(num2>0)||(num3>0)||(num4>0)||(num5>0)||(num6>0)){
            map.put("info","删除成功");
        }else{
            map.put("info","删除失败");
        }
        return map;
    }
}
